package com.uyii.virtualthread.demos.web;

import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Service
public class VirtualThreadService {

    private final ExecutorService executorService;

    private static final int TASK_COUNT = 100_000;

    public VirtualThreadService() {
        // 创建虚拟线程池
        executorService = Executors.newVirtualThreadPerTaskExecutor();
    }

    public void processTasks() {
        // 模拟多个并发任务
        for (int i = 0; i < 100; i++) {
            executorService.submit(() -> {
                try {
                    // 模拟处理任务
                    Thread.sleep(1000);
                    System.out.println("任务完成：" + Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }
    }

    public void processAsyncTasks() {
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("任务1完成：" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, executorService);

        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("任务2完成：" + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, executorService);

        // 等待所有任务完成
        CompletableFuture.allOf(future1, future2).join();
        System.out.println("所有任务已完成");
    }

    public void testTraditionalThreads() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        long startTime = System.nanoTime();
        for (int i = 0; i < TASK_COUNT; i++) {
            executorService.submit(() -> {
                // 执行简单任务
            });
        }
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
        long endTime = System.nanoTime();
        System.out.println("传统线程池执行时间：" + (endTime - startTime) / 1_000_000 + " ms");
    }

    public void testVirtualThreads() {
        ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor();
        long startTime = System.nanoTime();
        for (int i = 0; i < TASK_COUNT; i++) {
            executorService.submit(() -> {
                // 执行简单任务
            });
        }
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            // 等待任务完成
        }
        long endTime = System.nanoTime();
        System.out.println("虚拟线程池执行时间：" + (endTime - startTime) / 1_000_000 + " ms");
    }
}